/*
 * Copyright 2016 SoChishun.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package v.employee;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import m.EmployeeModels;
import ucomponse.SuePaginationCallback;
import ucomponse.SuePaginationConf;

/**
 *
 * @author SoChishun
 * @version 1.0 2016-6-22 SoChishun Added.
 */
public class FrmEmployeeList extends javax.swing.JFrame {

    /**
     * Creates new form FrmEmployeeList
     */
    public FrmEmployeeList() {
        initComponents();
        String string=util.URLUtil.getUrlContent("http://117.25.177.102:8899/XCrm/index.php/XApi/Seat/get_seat_list_json");
        final Object[] columnNames = new Object[]{"编号", "姓名", "性别", "分机号", "在线状态", "入职状态"};
        final Object[][] rowData = {
            {1, "张三", "男", "301", "在线", "正常"},
            {2, "李四", "男", "302", "在线", "正常"},
            {3, "王五", "男", "303", "在线", "正常"},
            {4, "小红", "女", "304", "在线", "正常"},
            {5, "鲁迅", "男", "305", "在线", "正常"},};
        DefaultTableModel tableModel = (DefaultTableModel) tbl_main.getModel();
        tableModel.setColumnCount(0);
        tableModel.setRowCount(0);
        TableColumn column;
        for (Object nameObject : columnNames) {
            column = new TableColumn();
            column.setHeaderValue(nameObject);
            tableModel.addColumn(nameObject);
        }
        // 隐藏编号列 2016-6-22
        TableColumn col0 = tbl_main.getColumnModel().getColumn(0);
        col0.setWidth(0);
        col0.setMaxWidth(0);
        col0.setMinWidth(0);
        col0.setPreferredWidth(0);
        for (Object[] row : rowData) {
            tableModel.addRow(row);
        }
        // 分页设置 2016-6-21
        int recordCount = 1000;
        int pageSize = 20;
        suePagination1.setPagination(recordCount, pageSize);
        suePagination1.setPage(1);
        suePagination1.setPagingAction(new SuePaginationCallback() {
            @Override
            public void setPage(int page) {
                SuePaginationConf pagingConf = new SuePaginationConf();
                pagingConf.pageId = page;
                EmployeeModels models = new EmployeeModels();
                Object[][] rowData = models.getPagingList(pagingConf);
                tableModel.setRowCount(0);
                for (Object[] row : rowData) {
                    tableModel.addRow(row);
                }
            }
        });
        JPopupMenu popupMenu = new JPopupMenu();
        String[] menuStrings = new String[]{"编辑", "删除", "复制"};
        JMenuItem menuItem;
        for (String menuString : menuStrings) {
            menuItem = new JMenuItem(menuString);
            menuItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int[] selRowsIndex = tbl_main.getSelectedRows();
                    for (int i : selRowsIndex) {
                        String cellValue = String.valueOf(tableModel.getValueAt(i, 0));
                        JOptionPane.showMessageDialog(null, cellValue);
                    }

                }
            });
            popupMenu.add(menuItem);
        }
        tbl_main.setComponentPopupMenu(popupMenu);
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        suePagination1 = new ucomponse.SuePagination();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbl_main = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tbl_main.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tbl_main);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(suePagination1, javax.swing.GroupLayout.DEFAULT_SIZE, 589, Short.MAX_VALUE)
                    .addComponent(jScrollPane2))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(suePagination1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmEmployeeList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmEmployeeList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmEmployeeList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmEmployeeList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmEmployeeList().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane2;
    private ucomponse.SuePagination suePagination1;
    private javax.swing.JTable tbl_main;
    // End of variables declaration//GEN-END:variables
}
